package com.study03;

import java.util.LinkedList;

public class Store{
    public LinkedList<Integer> linkedList = new LinkedList<>();
    public Integer maxLenth = 10;

    public void save(int number) {
        synchronized (linkedList){
            if(this.linkedList.size() == maxLenth){
                try {
                    linkedList.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                linkedList.add(number);
                System.out.println("生产者放入了" + number);
                linkedList.notifyAll();
            }
        }
    }

    public void take() {
        synchronized (linkedList){
            if(this.linkedList.size() == 0){
                try {
                    linkedList.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                int num = this.linkedList.poll();
                System.out.println("消费者取出了" + num);
                linkedList.notifyAll();
            }
        }
    }
}
